<?php
namespace app\applet\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class UnitBuilding extends Model
{
    /**
     * 单元楼添加时候显示的栋和单元
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitList(){
        $data=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('tung_num')
            ->find();
        $date=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('unit_num')
            ->find();
        //判断设置没有   没有返回2
        if($data!=null && $date!=null){
            $arr['tung']=$data['tung_num'];//栋
            $arr['unit']=$date['unit_num'];//单元
            return $arr;
        }else{
            return 2;
        }
    }
    /**
     * 单元楼添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitAdd($data){
        //处理数据
        $arr['unit_name'] = $data['unit_name'];
        $arr['tung_num'] = $data['tung_num'];
        $arr['unit_num'] = $data['unit_num'];
        $arr['layer_num'] = $data['layer_num'];
        $arr['area'] = $data['area'];
        $arr['floor_type']=json_encode($data['floor_type']);
       // var_dump($arr);exit;
        $arr['u_a_timg'] = time();//
        $where['tung_num']=$data['tung_num'];
        $where['unit_num']=$data['unit_num'];
        $i=Db::name('unit_building')->where($where)->find();
        if($i!=null){
            return false;
        }
        $data=Db::name('unit_building')->insert($arr);
        return $data;
    }
    /**
     * 单元楼显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitOne($id){
        //处理数据
        $data=Db::name('unit_building')
            ->field('unit_id,unit_name,tung_num,unit_num,layer_num,area,floor_type')
            ->where(['unit_id'=>$id])
            ->where(['u_type'=>1])
            ->find();
        $u= json_decode($data['floor_type']);
        $data['floor_type']=$u;
        return $data;
    }
    /**
     * 单元楼修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitUpdate($data){
        //处理数据
        $where['unit_id']=$data['unit_id'];
        $arr['unit_name'] = $data['unit_name'];
        $arr['tung_num'] = $data['tung_num'];
        $arr['unit_num'] = $data['unit_num'];
        $arr['layer_num'] = $data['layer_num'];
        $arr['area'] = $data['area'];
        $arr['floor_type']=json_encode($data['floor_type']);
      //  $arr['floor_type'] = implode(',',$data['floor_type']);
        $arr['u_u_time'] = time();//
        $data=Db::name('unit_building')->where($where)->update($arr);
        return $data;
    }
    /**
     * 单元楼显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitIndex(){
        $data = input();
        $page=isset($data['page']) ? trim($data['page']) : 1;
        $where['u_type']=1;
        $count=Db::name('unit_building')
            ->where($where)
            ->count();
        $data=Db::name('unit_building')
            ->field('unit_id,unit_name,tung_num,unit_num,layer_num,area,floor_type')
            ->where($where)
            ->page($page, 6)
            ->order('tung_num asc,unit_num asc,layer_num asc')
            ->select();
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }
    /**
     * 单元楼显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitIndex2($create_id=''){
        $where['u_type']=1;
        if (!empty($create_id)) {
            $where['create_id'] = $create_id;
        }
        $data=Db::name('unit_building')
            ->field('unit_id,unit_name,tung_num,unit_num,layer_num,area,floor_type')
            ->where($where)
            ->order('tung_num asc,unit_num asc,layer_num asc')
            ->select();
        $date['data']=$data;
        return $date;
    }
    /**
     * 单元楼删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function UnitDel($id){
        $data=Db::name('unit_building')
            ->where(['unit_id'=>$id])
            ->update(['u_type'=>2]);
        return $data;
    }

}